<template>
  <div class="login-container">
    <!-- GIF背景图 -->
    <div class="gif-background"></div>
    
    <!-- 半透明遮罩 -->
    <div class="overlay"></div>
    
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
      <div class="title-container">
        <h3 class="title">消防CRT管理平台·登录</h3>
        <p class="subtitle">智慧消防 · 安全守护</p>
      </div>

      <el-form-item prop="username" class="input-item">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.username"
          placeholder="账号"
          clearable
          type="text"
        />
      </el-form-item>

      <el-form-item class="input-item" prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="密码"
          clearable
          name="password"
          auto-complete="off"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-row :gutter="10">
        <el-col :span="14">
          <el-form-item prop="captcha" class="input-item">
            <span class="svg-container">
              <svg-icon icon-class="captcha" />
            </span>
            <el-input
              ref="captcha"
              v-model="loginForm.captcha"
              placeholder="请输入验证码"
              clearable
              type="text"
            />
          </el-form-item>
        </el-col>
        <el-col :span="10" class="captcha-col">
          <img :src="captcha_img" alt="验证码" @click="refresh_captcha" class="captcha-image">
        </el-col>
      </el-row>
      
      <el-button :loading="loading" native-type="submit" type="primary" class="login-btn" @click.native.prevent="handleLogin">
        {{ loading ? '登录中...' : '登录系统' }}
      </el-button>

      <!-- 备案信息 -->
      <div class="footer">
        消防云管理平台—智慧消防管理云平台系统—京ICP备14024677号-46
      </div>
    </el-form>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import md5 from 'js-md5'
import { getCaptcha } from '@/api/user'

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('账号错误'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能小于六位'))
      } else {
        callback()
      }
    }
    return {
      captcha_img: '',
      loginForm: {
        username: '',
        password: '',
        captcha: '',
        unique_id: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }],
        captcha: [{ required: true, trigger: 'blur', message: "验证码不得为空" }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    getCaptchaImg(){
      getCaptcha().then(res=>{
        this.captcha_img = res.data.captcha
        this.loginForm.unique_id = res.data.unique_id
      });
    },
    refresh_captcha(){
      this.getCaptchaImg()
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', {
            name: this.loginForm.username,
            password: md5(this.loginForm.password),
            captcha: this.loginForm.captcha,
            unique_id: this.loginForm.unique_id
          }).then((res) => {
            this.$router.push({ path: this.redirect || '/' })
            this.$message.success(res.msg)
          }).catch((error) => {
            this.getCaptchaImg();
          }).finally((res) => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  },
  created() {
    this.getCaptchaImg();
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  min-height: 100vh;
  width: 100%;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
}


/* GIF背景图 */
.gif-background {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: url('http://xfapi.samber.cn/static/imgs/login_bg.gif') center/cover no-repeat;
  z-index: -3;
  /* 如果GIF图太大，可以添加模糊效果 */
  //  filter: blur(2px); 
}

/* 半透明遮罩，确保文字可读性 */
.overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(12, 17, 32, 0.1); /* 深色半透明遮罩 */
  z-index: -2;
}

/* 登录表单 */
.login-form {
  width: 440px;
  padding: 40px;
  background: rgba(13, 25, 42, 0.85);
  border-radius: 16px;
  box-shadow: 0 0 50px rgba(0, 170, 255, 0.3);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(66, 133, 244, 0.4);
  position: relative;
  z-index: 1;

  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 4px;
    background: linear-gradient(90deg, #00aaff, #3a7bd5, #00aaff);
    box-shadow: 0 0 15px rgba(0, 170, 255, 0.6);
  }
}

.title-container {
  text-align: center;
  margin-bottom: 35px;
  
  .title {
    font-size: 26px;
    color: #fff;
    font-weight: bold;
    letter-spacing: 2px;
    text-shadow: 0 0 15px rgba(0, 170, 255, 0.8);
    margin-bottom: 8px;
  }
  
  .subtitle {
    color: #889aa4;
    font-size: 14px;
    text-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
  }
}

/* 输入项样式 */
.input-item {
  margin-bottom: 28px;
  border: 1px solid rgba(66, 133, 244, 0.3);
  background: rgba(13, 25, 42, 0.6);
  border-radius: 6px;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;

  &:hover {
    border-color: rgba(0, 170, 255, 0.6);
    box-shadow: 0 0 20px rgba(0, 170, 255, 0.3);
  }
}

.svg-container {
  padding: 0 12px;
  color: #889aa4;
  display: inline-block;
  flex-shrink: 0;
}
::v-deep  .el-input__inner:focus{
      border: none!important;
    }
/* 输入框样式 */
::v-deep .el-input {
  flex: 1;
 
  .el-input__inner {
    background: transparent !important;
    border: none !important;
    color: #fff !important;
    height: 48px;
    padding: 12px 5px 12px 0;
    caret-color: #00aaff;
    
    &::placeholder {
      color: #8a9bb2 !important;
    }
   
    &:-webkit-autofill,
    &:-webkit-autofill:hover, 
    &:-webkit-autofill:focus {
      -webkit-text-fill-color: #fff !important;
      -webkit-box-shadow: 0 0 0px 1000px #0d192a inset !important;
      transition: background-color 5000s ease-in-out 0s;
      border: none!important;
    }
  }
}

.show-pwd {
  position: absolute;
  right: 30px;
  color: #889aa4;
  cursor: pointer;
  font-size: 16px;
  flex-shrink: 0;

  &:hover {
    color: #00aaff;
  }
}

/* 验证码区域 */
.captcha-col {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 48px;
}

.captcha-image {
  height: 100%;
  width: 100%;
  border-radius: 4px;
  object-fit: cover;
  cursor: pointer;
  border: 1px solid rgba(66, 133, 244, 0.3);
}

/* 登录按钮 */
.login-btn {
  width: 100%;
  height: 48px;
  background: linear-gradient(90deg, #00aaff, #3a7bd5);
  border: none;
  border-radius: 6px;
  color: #fff;
  font-size: 16px;
  font-weight: bold;
  letter-spacing: 2px;
  margin-top: 10px;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;

  &:hover {
    background: linear-gradient(90deg, #00b4ff, #4a85e0);
    box-shadow: 0 0 25px rgba(0, 170, 255, 0.5);
    transform: translateY(-2px);
  }

  &:active {
    transform: translateY(0);
  }

  &::after {
    content: '';
    position: absolute;
    top: -50%;
    left: -50%;
    width: 200%;
    height: 200%;
    background: linear-gradient(45deg, transparent, rgba(255, 255, 255, 0.2), transparent);
    transform: rotate(45deg);
    animation: btnShine 3s infinite;
  }
}

@keyframes btnShine {
  0% {
    transform: rotate(45deg) translateX(-100%);
  }
  100% {
    transform: rotate(45deg) translateX(100%);
  }
}

/* 备案信息 */
.footer {
  text-align: center;
  margin-top: 30px;
  color: #5e81ac;
  font-size: 12px;
  padding: 15px;
  border-top: 1px solid rgba(66, 133, 244, 0.2);
  text-shadow: 0 0 5px rgba(0, 0, 0, 0.5);
}

/* 表单内容布局 */
::v-deep .el-form-item__content {
  display: flex;
  align-items: center;
  width: 100%;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .login-form {
    width: 90%;
    padding: 30px 20px;
  }
  
  .title {
    font-size: 22px;
  }
  
  ::v-deep .el-input .el-input__inner {
    font-size: 14px;
  }
  
  /* 移动端优化GIF背景 */
  .gif-background {
    background-attachment: fixed;
  }
}

/* 如果GIF加载失败时的备用背景 */
.gif-background {

  // background: linear-gradient(125deg, #0c1120, #0a1a2a, #0c1120);
}

.gif-background::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: 
    radial-gradient(circle at 20% 30%, rgba(41, 130, 185, 0.3) 0%, transparent 50%),
    radial-gradient(circle at 80% 70%, rgba(94, 53, 177, 0.3) 0%, transparent 50%);
}
</style>